package week02

// 78. 子集
// https://leetcode-cn.com/problems/subsets/

func subsets(nums []int) [][]int {
	var ans [][]int
	ans = subsets2(nums, []int{}, ans, len(nums)-1)
	return ans
}

func subsets2(nums, temp []int, ans[][]int, index int) [][]int {
	if -1 == index {
		var res = make([]int, len(temp))
		copy(res, temp)	// 防止对temp的操作影响到已经确定好的结果
		ans = append(ans, res)
		return ans
	}
	ans = subsets2(nums, temp, ans, index-1)
	temp = append(temp, nums[index])
	ans = subsets2(nums, temp, ans, index-1)
	temp = temp[:len(temp)-1]
	return ans
}